﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq.Expressions;
using TSharp.Core.Pattern;

namespace TSharp.Core
{
    /// <summary>
    /// 对象比较器
    /// <para>by tangjingbo at 2011/10/9 23:50</para>
    /// </summary>
    public class CommonComparer
    {
        /// <summary>
        /// Gets the ordinal ignore case.
        /// </summary>
        public static IComparer<string> OrdinalIgnoreCase
        {
            get
            {
                return New<string>((x, y) => string.Compare(x, y, StringComparison.OrdinalIgnoreCase));
            }
        }

        /// <summary>
        /// Gets the current culture ignore case.
        /// </summary>
        public static IComparer<string> CurrentCultureIgnoreCase
        {
            get
            {
                return New<string>((x, y) => string.Compare(x, y, StringComparison.CurrentCultureIgnoreCase));
            }
        }
        /// <summary>
        /// News the specified compare.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="compare">The compare.</param>
        /// <returns></returns>
        public static IComparer<T> New<T>(Func<T, T, int> compare)
        {
            return new CommonComparer<T>(compare);
        }
        /// <summary>
        /// Clones this instance.
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            return this.MemberwiseClone();
        }
    }
    /// <summary>
    /// 对象比较器
    /// <para>by tangjingbo at 2011/10/9 23:50</para>
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class CommonComparer<T> : IComparer<T>, ICloneable
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="CommonComparer&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="compare">The compare.</param>
        public CommonComparer(Func<T, T, int> compare)
        {
            this.compare = compare;
        }
        private readonly Func<T, T, int> compare;
        int IComparer<T>.Compare(T x, T y)
        {
            return compare(x, y);
        }

        /// <summary>
        /// 创建作为当前实例副本的新对象。
        /// </summary>
        /// <returns>
        /// 作为此实例副本的新对象。
        /// </returns>
        public object Clone()
        {
            return this.MemberwiseClone();
        }
    }
}